package src.itheima03;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author xxx
 * @date 2020/12/22 19:47
 * @description
 */
public class Test {
    public static void main(String[] args) throws IOException {
        //使用字符输入流从指定文件读取内容，再通过字符输出流书写到另一个文件中
        FileReader fileReader=new FileReader("D:\\java349\\day11\\from\\f1.txt");
        FileWriter fileWriter=new FileWriter("D:\\java349\\day11\\dest\\f1.txt");

        char[] chars=new char[1024];
        int a=0;
        while ((a=fileReader.read(chars))!=-1){
            fileWriter.write(chars,0,a);
        }
        fileReader.close();
        fileWriter.close();

        //使用 try。。。catch。。。进行文件字符输入输出流进行内容复制，注意空指针异常


        method();
    }



    public  static void  method()  {
        FileReader fileReader = null;
        FileWriter fileWriter=null;

        try{
            fileReader=new FileReader("D:\\java349\\day11\\from\\f1.txt");
            fileWriter=new FileWriter("day11/dest/f2.txt");

            char[] chars=new char[1024];
            int a=0;

            while ((a=fileReader.read(chars))!=-1){
               fileWriter.write(chars,0,a);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           if(fileReader!=null){
             try {
                 fileReader.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
           }

           if (fileWriter!=null){
               try {
                   fileWriter.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }
}
